package com.cloudcc.mobile.im.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.easemob.chat.EMGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 群组列表
 * @author wangzhiqiang
 * @date 2015-10-15 下午8:35:42
 * 
 */
public class GroupAdapter extends ArrayAdapter<EMGroup> {

	private LayoutInflater inflater;
	private String newGroup;
	private String addPublicGroup;

	public GroupAdapter(Context context, int res, List<EMGroup> groups) {
		super(context, res, groups);
		this.inflater = LayoutInflater.from(context);
		newGroup = context.getResources()
				.getString(R.string.The_new_group_chat);
		addPublicGroup = context.getResources().getString(
				R.string.add_public_group_chat);
		this.groups = groups;
	}

	@Override
	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return 0;
		} else if (position == 1) {
			return 1;
		} else {
			return 2;
		}
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (getItemViewType(position) == 0) {
			if (convertView == null) {
				convertView = inflater.inflate(
						R.layout.em_search_bar_with_padding, null);
			}
			final EditText query = (EditText) convertView
					.findViewById(R.id.query);
			final TextView sousuo_hint = (TextView) convertView
					.findViewById(R.id.sousuo_hint);
			final ImageButton clearSearch = (ImageButton) convertView
					.findViewById(R.id.search_clear);
			query.addTextChangedListener(new TextWatcher() {
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					getFilter().filter(s);
					if (s.length() > 0) {
						sousuo_hint.setVisibility(View.INVISIBLE);
						clearSearch.setVisibility(View.VISIBLE);
					} else {
						sousuo_hint.setVisibility(View.VISIBLE);
						clearSearch.setVisibility(View.INVISIBLE);
					}
				}

				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
				}

				public void afterTextChanged(Editable s) {



				}
			});
			clearSearch.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					query.getText().clear();
				}
			});
		}
		//创建群聊
//		else if (getItemViewType(position) == 1) {
//			if (convertView == null) {
//				convertView = inflater.inflate(R.layout.em_row_add_group, null);
//			}
//			((ImageView) convertView.findViewById(R.id.avatar))
//					.setImageResource(R.drawable.em_create_group);
//			((TextView) convertView.findViewById(R.id.name)).setText(newGroup);
//			((TextView) convertView.findViewById(R.id.header))
//					.setVisibility(View.VISIBLE);
//		}
		else {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.em_row_group, null);
			}
			((TextView) convertView.findViewById(R.id.name)).setText(getItem(
					position - 1).getGroupName());


		}

		return convertView;
	}

	@Override
	public int getCount() {
		return super.getCount() + 1;
	}

	public Filter getFilter() {
		if (mFilter == null) {
			mFilter = new ArrayFilter();
		}
		return mFilter;
	}

	private ArrayList<EMGroup> mOriginalValues;
	private ArrayFilter mFilter;
	private final Object mLock = new Object();
	private List<EMGroup> groups;
	private class ArrayFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence prefix) {
			FilterResults results = new FilterResults();

			if (mOriginalValues == null) {
				synchronized (mLock) {
					mOriginalValues = new ArrayList<EMGroup>(groups);
				}
			}

			if (prefix == null || prefix.length() == 0) {
				ArrayList<EMGroup> list;
				synchronized (mLock) {
					list = new ArrayList<EMGroup>(mOriginalValues);
				}
				results.values = list;
				results.count = list.size();
			} else {
				String prefixString = prefix.toString().toLowerCase();

				ArrayList<EMGroup> values;
				synchronized (mLock) {
					values = new ArrayList<EMGroup>(mOriginalValues);
				}

				final int count = values.size();
				final ArrayList<EMGroup> newValues = new ArrayList<EMGroup>();

				for (int i = 0; i < count; i++) {
					final EMGroup value = values.get(i);
					final String valueText = value.toString().toLowerCase();

					// First match against the whole, non-splitted value
					if (valueText.contains(prefixString)) {
						newValues.add(value);
					} else {
						final String[] words = valueText.split(" ");
						final int wordCount = words.length;

						// Start at index 0, in case valueText starts with space(s)
						for (int k = 0; k < wordCount; k++) {
							if (words[k].contains(prefixString)) {
								newValues.add(value);
								break;
							}
						}
					}
				}

				results.values = newValues;
				results.count = newValues.size();
			}

			return results;
		}

		@Override
		protected void publishResults(CharSequence constraint, FilterResults results) {
			//noinspection unchecked
			groups = (List<EMGroup>) results.values;
			clear();
			addAll(groups);
			if (results.count > 0) {
				notifyDataSetChanged();
			} else {
				notifyDataSetInvalidated();
			}
		}
	}




}
